package com.huonan.jb.tools.esuse.export.content;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.setting.dialect.Props;
import cn.hutool.setting.dialect.PropsUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.huonan.jb.tools.esuse.export.Constants;
import com.huonan.jb.tools.esuse.export.filter.FieldsFilter;
import com.huonan.utils.JacksonUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * @Description 抽取字段
 * @Author lxp
 * @Date 2023/2/6 下午2:29
 * @Version V1.0
 **/
@Slf4j
public class ExtractFields {

    public String listToStringBuild(List<String> list) {
        StringJoiner joiner = new StringJoiner("\n");
        for (String s : list) {
            joiner.add(s);
        }
        return joiner.toString();
    }

    private String checkNull(String filed) {
        return Optional.ofNullable(filed).orElse("");
    }

    public List<String> extractJsonFields(JSONArray json, boolean needIndex) {
        List<String> list = new ArrayList<>();
        if (json.size() > 0) {
            for (Object element : json) {
                JSONObject jsonObject = JSONUtil.parseObj(element.toString());
                Map<String, Object> fieldMap = new LinkedHashMap<>();
                String id = jsonObject.getStr("_id");
                String index = jsonObject.getStr("_index");
                String type = jsonObject.getStr("_type");
                List<String> sourceFields = sourceFields();
                if (needIndex) {
                    fieldMap.put("_index", index);
                    fieldMap.put("_id", id);
                    fieldMap.put("_type", type);
                } else {
                    sourceFields.remove("_id");
                    sourceFields.remove("_index");
                }
                JSONObject source = jsonObject.getJSONObject("_source");
                if (ObjectUtil.isNotEmpty(source)) {
                    fieldMap = FieldsFilter.filter(source, fieldMap, false);
                }
                if (MapUtil.isNotEmpty(fieldMap) && fieldMap.size() >= 2) {
                    try {
                        String mapToJson = JacksonUtil.convertMapToJson(fieldMap);
                        list.add(mapToJson);
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return list;
    }

    public List<String> aggExtractJsonFields(JSONArray json, boolean needIndex) {
        List<String> list = new ArrayList<>();
        if (json.size() > 0) {
            for (Object element : json) {
                JSONObject jsonObject = JSONUtil.parseObj(element.toString());
                String source = jsonObject.getStr("key");
                list.add(source);
            }
        }
        return list;
    }

    public List<String> extractCsvFields(JSONArray json, boolean needIndex) {
        List<String> list = new ArrayList<>();
        StringJoiner sb;
        if (json.size() > 0) {
            for (Object element : json) {
                sb = new StringJoiner(",");
                JSONObject jsonObject = JSONUtil.parseObj(element.toString());
                if (needIndex) {
                    String index = jsonObject.getStr("_index");
                    String id = jsonObject.getStr("_id");
                    sb.add(index).add(id);
                }
                List<String> sourceFields = sourceFields();
                JSONObject source = jsonObject.getJSONObject("_source");
                sourceFields.remove("_index");
                sourceFields.remove("_id");
                for (String key : sourceFields) {
                    String value = source.getStr(key);
                    if (StrUtil.isNotEmpty(value)) {
                        sb.add(value);
                    }
                }
                list.add(sb.toString());
            }
        }
        return list;
    }


    /*指定导出字段*/
    public List<String> sourceFields() {
        Props props = PropsUtil.get(Constants.SQL_FILE);
        String sourceField = props.getStr("sql");
        JSONObject entries = JSONUtil.parseObj(sourceField);
        JSONArray source = entries.getJSONArray("_source");
        return Arrays.asList("kw_uid", "kw_nickName");
        // return source.stream().map(Object::toString).collect(Collectors.toList());
    }
}
